package com.intellagric.service.impl.module.cms_content;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.cms.ContentDao;
import com.intellagric.mapper.CmsCategoryContentMapper;
import com.intellagric.mapper.CmsCategoryMapper;
import com.intellagric.module.cms.ContentVo;
import com.intellagric.pojo.CmsCategory;
import com.intellagric.pojo.CmsCategoryContent;
import com.intellagric.pojo.CmsCategoryContentExample;
import com.intellagric.service.module.cms_content.ContentCategoryService;
import com.intellagric.service.module.cms_content.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2018/10/19
 * @Description: 内容管理服务
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private CmsCategoryContentMapper contentMapper;

    @Autowired
    private ContentDao contentDao;

    @Autowired
    private ContentCategoryService contentCategoryService;

    /**
     * 添加内容信息
     * @param content
     * @return int
     */
    @Override
    public ResponseMessage addContent(CmsCategoryContent content) {
        if (contentMapper.insertSelective(content) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除内容信息
     * @param id
     * @return int
     */
    @Override
    public ResponseMessage deleteContent(String id) {
        if (contentMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改内容信息
     * @param content
     * @return int
     */
    @Override
    public ResponseMessage editContent(CmsCategoryContent content) {
        if (contentMapper.updateByPrimaryKeySelective(content) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询单个内容信息
     * @param id
     * @return Content
     */
    @Override
    public CmsCategoryContent queryContentById(String id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    /**
    * @Author:         Ade
    * @CreateDate:     2019/2/25 11:10
    * 根据分类id查询内容集合
    */
    @Override
    public List<CmsCategoryContent> queryContentsByCategoryId(String categoryId) {

        CmsCategoryContentExample  contentExample=new CmsCategoryContentExample();
        //这里的title内容为1、2、3....表示显示顺序
        contentExample.setOrderByClause("sub_title asc");
        contentExample.createCriteria().andCategoryIdEqualTo(categoryId);

        return contentMapper.selectByExampleWithBLOBs(contentExample);
    }


    /**
     * 查询所有内容信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult queryContentList() {
        CmsCategoryContentExample contentExample = new CmsCategoryContentExample();
        contentExample.createCriteria().andIdIsNotNull();
        List<CmsCategoryContent> categoryContentList = contentMapper.selectByExampleWithBLOBs(contentExample);
        return LayuiDataGridResult.success().add(categoryContentList,categoryContentList.size());
    }

    /**
     * 分页查询所有内容信息
     * @return List<Content>
     */
    @Override
    public LayuiDataGridResult queryContentPageList(int page,int limit) {
        PageHelper.startPage(page,limit);
        CmsCategoryContentExample contentExample = new CmsCategoryContentExample();
        contentExample.createCriteria().andIdIsNotNull();
        List<CmsCategoryContent> categoryContentList = contentMapper.selectByExampleWithBLOBs(contentExample);
        PageInfo<CmsCategoryContent> pageInfo = new PageInfo<>(categoryContentList);
        return LayuiDataGridResult.success().add(categoryContentList,pageInfo.getTotal());
    }

    /**
     * 根据ContentVo类查询内容信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult queryContentByVo(int page,int limit,ContentVo contentVo) {
        PageHelper.startPage(page,limit);
        List<CmsCategoryContent> categoryContentList = contentDao.selectContentByVo(contentVo);
        PageInfo<CmsCategoryContent> pageInfo = new PageInfo<>(categoryContentList);
        return LayuiDataGridResult.success().add(categoryContentList,pageInfo.getTotal());
    }


}
